# 改变duty circle 实现颜色的变化
import RPi.GPIO as GPIO
import time
from raspberry05_pwm_4_led import change_dt


# def change_dt(color_list):
#     '''
#      将0-255更改为0-100并返回
#     '''
#     return int(100-(255-color_list[0])/255*100),int(100-(255-color_list[1])/255*100),int(100-(255-color_list[2])/255*100)

def start():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    # 11:红 12:绿 13:蓝
    GPIO.setup((11,12,13),GPIO.OUT)
    frequency_green = 2000
    frequency_red = 2000
    frequency_blue = 5000
    global pwm_list
    pwm_list = [GPIO.PWM(11,frequency_green), GPIO.PWM(12,frequency_red), GPIO.PWM(13,frequency_blue)]
    for p in pwm_list:
        p.start(0)

def end():
    for p in pwm_list:
        p.stop()
    GPIO.cleanup()


def change_color(colors):
    print(colors)
    dc_list=change_dt(colors)
    for i in range(3):
        pwm_list[i].ChangeDutyCycle(dc_list[i])
    time.sleep(1)

def get_input():
    i = 'start'
    r = g = b = 255
    while i != 'q':
        i = input('input r,g,b:')
        if i=='r':
            r+=10
        elif i=='g':
            g+=10
        elif i=='b':
            b+=10
        if i=='R':
            r-=10
        elif i=='G':
            g-=10
        elif i=='B':
            b-=10
        # print(r,g,b)
        change_color((r,g,b))
    

if __name__ == '__main__':
    color =255,255,0
    colors=change_dt((color))
    print(colors)
    try:
        start()
        get_input()
    except KeyboardInterrupt:
        print('bye')
        end()

